//
//  WorkQueryViewController.m
//  homeland
//
//  Created by SunShine on 13-11-13.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "WorkQueryViewController.h"
#import "WorkQueryIconButton.h"
#import "ListViewController.h"

#define NUMBER_OF_BUTTONS 2
#define ICON_BUTTON_HEIGHT 120
#define ICON_VERTICAL_SPACE 10

@interface WorkQueryViewController ()
@property (strong, nonatomic) NSArray *imageList;
@property (strong, nonatomic) NSArray *titleList;
@property (strong, nonatomic) NSArray *contentList;
@property (strong, nonatomic) NSArray *additionalList;
@end

@implementation WorkQueryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"办事及指南查询";
    //数据
    self.imageList = [NSArray arrayWithObjects:@"二级菜单按钮-办事查询-居民办事查询", @"二级菜单按钮-办事查询-企业办事查询", nil];
    self.titleList = [NSArray arrayWithObjects:@"居民办事查询", @"企业办事查询", nil];
    self.contentList = [NSArray arrayWithObjects:@"查询居民可到区政府部门办理的服务事项指南，已受理事项进度", @"查询企业可到区政府办理的服务事项指南，已受理事项进度", nil];
    self.additionalList = [NSArray arrayWithObjects:@"数据来源于海珠区社区服务管理信息平台", @"数据来源于海珠区\"一站式\"行政审批和政务服务信息平台", nil];
    [self drawButtons];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Draw Method
-(void)drawButtons
{
    for (int i = 0; i < NUMBER_OF_BUTTONS; i++) {
        CGFloat minY = i*ICON_BUTTON_HEIGHT + (i+1)*ICON_VERTICAL_SPACE;
        WorkQueryIconButton *bt = [[[NSBundle mainBundle] loadNibNamed:@"WorkQueryIconButton" owner:self options:nil] objectAtIndex:0];
        bt.image.image = [UIImage imageNamed:[_imageList objectAtIndex:i]];
        bt.titleLabel.text = [_titleList objectAtIndex:i];
        bt.contentLabel.text = [_contentList objectAtIndex:i];
        bt.additionalInfoLabel.text = [_additionalList objectAtIndex:i];
        bt.frame = CGRectMake(0, minY, 320, ICON_BUTTON_HEIGHT);
        bt.tag = i;
        //初始化触摸事件
        [bt addTarget:self action:@selector(tapIconEvent:) forControlEvents:UIControlEventTouchUpInside];
        [bt addTarget:self action:@selector(iconTouchedDown:) forControlEvents:UIControlEventTouchDown];
        [bt addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchCancel];
        [bt addTarget:self action:@selector(iconTouchCancel:) forControlEvents:UIControlEventTouchDragExit];
        [self.scrollView addSubview:bt];
    }
}

#pragma mark - Touch Methods
//icon的点击事件
-(void)tapIconEvent:(id)sender
{
    WorkQueryIconButton *icon = (WorkQueryIconButton *)sender;
    NSLog(@"click icon:%d", icon.tag);
    icon.backgroundColor = [UIColor clearColor];
    //进入下一级页面
    ListViewController *vc = [[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
    NSDictionary *vcConfig = nil;
    if (icon.tag == 0) {
        vcConfig = [NSDictionary dictionaryWithObjectsAndKeys:@"居民办事指南", @"key", nil];
    } else {
        vcConfig = [NSDictionary dictionaryWithObjectsAndKeys:@"企业办事指南", @"key", nil];
    }
//    NSDictionary *vcConfig = [NSDictionary dictionaryWithObjectsAndKeys:icon.titleLabel.text, @"key", nil];
    
    vc.config = vcConfig;
    [self.navigationController pushViewController:vc animated:YES];
}

//icon的按下事件，这里主要是变个颜色
-(void)iconTouchedDown:(id) sender
{
    WorkQueryIconButton *icon = (WorkQueryIconButton *)sender;
    icon.backgroundColor = [UIColor colorWithRed:0.50 green:0.68 blue:0.84 alpha:1.0];
    NSLog(@"icon %d touch down", icon.tag);
}

//icon的取消事件，包含拖拽出控件，这里主要是变回颜色
-(void)iconTouchCancel:(id)sender
{
    WorkQueryIconButton *icon = (WorkQueryIconButton *)sender;
    icon.backgroundColor = [UIColor clearColor];
    NSLog(@"icon %d touch cancel", icon.tag);
}
@end
